import Vue from 'vue'
import {doGetInfo, doLogin, doLogout} from '@/api/login'
import {clearVuexLocalStorage} from '@/store/sync-storage'
import {ACCESS_TOKEN} from '@/config/constants'


const user = {

  state: {
    id: null,
    name: '',
    nick: '',
    root: false,
    dept: {
      id: null,
      name: '',
    },
    role: {
      id: null,
      name: '',
      dataScope: null,
      accessibleViews: [],
      customAccessibleDepts: []
    },
  },

  mutations: {
    SET_ID: (state, id) => {
      state.name = id
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_NICK: (state, nick) => {
      state.nick = nick
    },
    SET_ROOT: (state, root) => {
      state.root = root
    },
    SET_DEPT: (state, userInfo) => {
      state.dept.id = userInfo.deptId
      state.dept.name = userInfo.deptName
    },
    SET_ROLE: (state, userInfo) => {
      state.role.id = userInfo.roleId
      state.role.name = userInfo.roleName
      state.role.dataScope = userInfo.dataScope
      state.role.accessibleViews = userInfo.accessibleViews
      state.role.customAccessibleDepts = userInfo.customAccessibleDepts
    },
    CLEAR_STATE: (state, userInfo) => {
      state.id = null
      state.name = null
      state.nick = null
      state.root = false
      state.dept = {
        id: null,
          name: '',
      }
      state.role = {
        id: null,
          name: '',
          dataScope: null,
          accessibleViews: [],
          customAccessibleDepts: []
      }
    },
  },

  actions: {

    // 登录
    login({commit}, formData) {
      return new Promise((resolve, reject) => {
        doLogin(formData).then(response => {
          // 在request.js的拦截器中已存入ACCESS_TOKEN，所以这里不用再存一次
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    getInfo({commit}) {
      return new Promise((resolve, reject) => {
        doGetInfo().then(resp => {
          const userInfo = resp.result.userInfo
          console.log('vuex, userInfo: %o',userInfo)
          commit('SET_ID', userInfo.id)
          commit('SET_NAME', userInfo.name)
          commit('SET_NICK', userInfo.nick)
          commit('SET_ROOT', userInfo.root)
          if (!userInfo.root) {
            commit('SET_DEPT', userInfo)
            commit('SET_ROLE', userInfo)
          }
          resolve(resp)
        }).catch(error => {
          // 清空LocalStorage中token信息
          Vue.ls.remove(ACCESS_TOKEN)
          // 清空vuex同步到LocalStorage的内容
          clearVuexLocalStorage()
          reject(error)
        })
      })
    },

    // 退出登录
    logout({commit, state}) {
      return new Promise((resolve) => {
        commit('CLEAR_STATE')
        // 清空LocalStorage中token信息
        Vue.ls.remove(ACCESS_TOKEN)
        // 清空vuex同步到LocalStorage的内容
        clearVuexLocalStorage()
        doLogout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    }

  }
}

export default user
